package com.snail.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.snail.model.dto.UpdatePasswordDTO;
import com.snail.model.dto.UserAssignRoleDTO;
import com.snail.model.pojo.SysUser;
import com.snail.response.BaseResponse;
import com.snail.response.ResultUtils;
import com.snail.service.SysUserRoleService;
import com.snail.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author 蔡徐坤
 * @Date 2025/7/14 20:14
 * @Description: 系统用户相关接口
 */
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;


    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 获取当前登录用户的信息
     */
    @GetMapping("/getUser")
    public BaseResponse<SysUser> getUser() {
        SysUser sysUser = sysUserService.getUser();
        return ResultUtils.success(sysUser);
    }

    /**
     * 查询所有用户
     */
    @GetMapping("/findAllUsers")
    public BaseResponse<List<SysUser>> findAllUsers() {
        System.out.println("拦截生效，当前登录id: " + StpUtil.getLoginId());
        System.out.println("当前用户角色: " + StpUtil.getRoleList());
        return ResultUtils.success(sysUserService.findAllUsers());
    }

    /**
     * 根据 ID 查询用户
     */
    @GetMapping("/getUserById")
    public BaseResponse<SysUser> getUserById(@RequestParam Long userId) {
        return ResultUtils.success(sysUserService.getByUserId(userId));
    }

    /**
     * 更新用户信息
     */
//    @SaCheckRole("admin")
    @PutMapping("/updateUser")
    public BaseResponse<String> updateUser(@RequestBody SysUser sysUser) {
        sysUserService.updateUser(sysUser);
        return ResultUtils.success("更新成功");
    }

    /**
     * 修改密码(用于用户个人信息自己修改密码)
     */
    @SaCheckRole("user")
    @PutMapping("/updatePassword")
    public BaseResponse<String> updatePassword(@RequestBody UpdatePasswordDTO userDTO) {
        sysUserService.updatePassword(userDTO);
        return ResultUtils.success("密码修改成功");
    }

    /**
     * 重置密码为默认密码（123456）
     */
    @SaCheckRole("admin")
    @PutMapping("/resetPassword/{id}")
    public BaseResponse<String> resetPassword(@PathVariable Long id) {
        sysUserService.resetPassword(id);
        return ResultUtils.success("密码重置成功");
    }

    /**
     * 删除用户（逻辑删除）
     */
    @DeleteMapping("/deleteUser/{id}")
    public BaseResponse<String> deleteUser(@PathVariable Long id) {
        sysUserService.deleteUser(id);
        return ResultUtils.success("删除成功");
    }

    /**
     * 分配角色给用户（覆盖旧绑定）
     */
    @PostMapping("/addUserToRole")
    public BaseResponse<String> addUserToRole(@RequestBody UserAssignRoleDTO request) {
        sysUserRoleService.addUserToRole(request.getUserId(), request.getRoleIds());
        return ResultUtils.success("角色分配成功");
    }

    /**
     * 查询用户角色 ID 列表
     */
    @GetMapping("/getRoleIds")
    public BaseResponse<List<Long>> getRoles(@RequestParam Long userId) {
        List<Long> roleIdsByUserId = sysUserRoleService.getRoleIdsByUserId(userId);
        return ResultUtils.success(roleIdsByUserId);
    }

    /**
     * 批量查询所有用户的角色ID映射
     */
    @GetMapping("/getAllUserRoles")
    public BaseResponse<Map<Long, List<Long>>> getAllUserRoles() {
        Map<Long, List<Long>> userRoleMap = sysUserRoleService.getAllUserRoleMap();
        return ResultUtils.success(userRoleMap);
    }


}
